<?php
namespace API\Controller;
use Think\Controller;
class ShopcartController extends CommonController {
	
	//将商品加入到购物车
	public function addgoodstocart(){
		//获取发送过来的商品信息
		$goodsid = I("post.goodsid");
		$goodsname = I("post.goodsname");
		$goodsimg = I("post.goodsimg");
		$goodsnow = I("post.goodsnow");
		$quantity = I("post.quantity");
		$quantity =  intval($quantity);
		$cartlist = M("Cartlist");
		$cartgoodslist = M("Cartgoodslist");
		$userid=$_SESSION['USERID'];
		//判断是否需要在购物车中插入新的一行
		$tag = 0;
		//查询是否有该用户的购物车信息，没有则创建
		$res=$cartlist->where("userid = '{$userid}'")->find();
		if (!$res) {
			$data = array(
				"userid"=>$userid,
				"createtime"=>time()
			);
			$ret=$cartlist->add($data);
			if (!$ret) {
				$this->response("创建购物车信息",false,0,"创建购物车信息失败");
			}else{
				$tag=0;
				$goodsdata = array(
					"cartid"=>$ret,
					"userid"=>$userid,
					"goodsid"=>$goodsid,
					"number"=>$quantity,
					"addtime"=>time()
				);
				$res=$cartgoodslist->add($goodsdata);
				$returngoods = array(
					"goodsid"=>$goodsid,
					"goodsname"=>$goodsname,
					"goodsimg"=>$goodsimg,
					"goodsnow"=>$goodsnow,
					"goodsnumber"=>$quantity
				);
			}	
		}else{
			//查询该商品是否已经存在于该用户购物车
			$goodsinf=$cartgoodslist->where("userid={$userid} AND goodsid={$goodsid}")->find();
			if (!$goodsinf) {
				$tag=0;
				$goodsdata = array(
					"cartid"=>$res['id'],
					"userid"=>$userid,
					"goodsid"=>$goodsid,
					"number"=>$quantity,
					"addtime"=>time()
				);
				$res=$cartgoodslist->add($goodsdata);
				$returngoods = array(
					"goodsid"=>$goodsid,
					"goodsname"=>$goodsname,
					"goodsimg"=>$goodsimg,
					"goodsnow"=>$goodsnow,
					"goodsnumber"=>$quantity
				);
			}else{
				$tag=1;
				$goodsdata = array(
					"number"=>$goodsinf['number'] + $quantity,
					"addtime"=>time()
				);
				$res=$cartgoodslist->where("userid={$userid} AND goodsid={$goodsid}")->save($goodsdata);
			}
			
		}

		if (!$res) {
			$this->response("加入到购物车商品信息列表",false,0,"添加到购物车失败");
		}else{

			if ($tag==0) {
				$this->response("加入到购物车商品信息列表",true,$tag,$returngoods);	
			}else{
				$this->response("加入到购物车商品信息列表",true,$tag,"成功将商品加入到购物车");	
			}
			
		}
	}

	//将商品移除购物车
	public function removegoodsfromcart(){
		$goodsid = I("post.goodsid");
		$cartgoodslist = M("Cartgoodslist");
		$userid=$_SESSION['USERID'];
		$res = $cartgoodslist->where("userid='{$userid}' AND goodsid ='{$goodsid}'")->delete();
		if ($res) {
			$this->response("将商品从购物车移除",true,1,"成功将商品从购物车移除");
		}else{
			$this->response("将商品从购物车移除",false,0,"将商品从购物车移除失败");
		}	
	}

	//在购物车页面改变商品数量
	public function changenumber(){
		$goodsid = I("post.goodsid");
		$goodsnumber = I("post.goodsnumber");
		if ($goodsnumber<0) {
			$this->response("购物车商品数量改变",false,0,"改变商品数量失败,商品数量不能为0");
		}
		$cartgoodslist = M("Cartgoodslist");
		$userid=$_SESSION['USERID'];
		$data =array(
			"number"=>$goodsnumber,
			"addtime"=>time()
		);
		$res = $cartgoodslist->where("userid='{$userid}' AND goodsid ='{$goodsid}'")->save($data);
		if ($res) {
			$this->response("购物车商品数量改变",true,1,"成功改变商品数量");
		}else{
			$this->response("购物车商品数量改变",false,0,"改变商品数量失败");
		}	
	}

	//删除购物车中的商品
	public function delgoods(){
		$userid=$_SESSION['USERID'];
		$cartgoodslist = M("Cartgoodslist");
		$goodsid = I("post.goodsid");
		if (is_array($goodsid)) {
			$ids = implode(',',$goodsid);  
			$map['goodsid'] = array('in',$ids);
			$map['userid'] = array('eq',$userid);
			$res = $cartgoodslist->where($map)->delete();
		}else{
			$res = $cartgoodslist->where("userid='{$userid}' AND goodsid ='{$goodsid}'")->delete();
		}
		if ($res) {
			$this->response("删除购物车中的商品",true,1,"成功删除购物车中的商品");
		}else{
			$this->response("删除购物车中的商品",false,0,"删除购物车中的商品失败");
		}
	}

	//将购物车中的被选中要结算的商品在数据库中改变一个字段
	public function paygoods(){
		$userid=$_SESSION['USERID'];
		$cartgoodslist = M("Cartgoodslist");
		$goodsid = I("post.goodsid");
		if (is_array($goodsid)) {
			$ids = implode(',',$goodsid);  
			$map['goodsid'] = array('in',$ids);
			$map['userid'] = array('eq',$userid);
			$res = $cartgoodslist->where($map)->setField('isselect','1');
		}
		if ($res) {
			$this->response("改变购物车中的商品",true,1,"成功");
		}else{
			$this->response("改变购物车中的商品",false,0,"失败");
		}
	}
	//生成订单信息
	public function createorder(){
		$userid=$_SESSION['USERID'];
		$goodslist = I("post.goodslist");
		$goodslist = json_encode($goodslist);
		$addressid = I("post.addressid");
		$payprices = I("post.payprices");
		$Order = M("Orderlist");
		$Orderdata = array(
			"userid"=>$userid,
			"goodslist"=>$goodslist,
			"addressid"=>$addressid,
			"payprices"=>$payprices,
			"createtime"=>time()
		);
		$res = $Order->add($Orderdata);
		if ($res) {
			$this->response("创建订单",true,1,$res);
		}else{
			$this->response("创建订单",false,0,"失败");
		}
	} 
	//商品加入收藏列表
	public function addgoodstocol(){
		$userid=$_SESSION['USERID'];
		$goodsid = I("post.goodsid");
		$Collection = M("Collection");
		//查询是否已经在收藏列表
		$res = $Collection->where("userid = {$userid} AND goodsid = {$goodsid}")->find();
		if ($res) {
			$this->response("加入收藏",true,0,"商品已经在收藏列表");
		}
		$data = array(
			"userid"=>$userid,
			"goodsid"=>$goodsid,
			"collectiontime"=>time()
		);
		$res=$Collection->add($data);
		if ($res) {
			$this->response("加入收藏",true,1,"加入收藏成功");
		}else{
			$this->response("加入收藏",false,0,"加入收藏失败");
		}
	}

	//将商品移除收藏列表
	public function delcol(){
		$userid=$_SESSION['USERID'];
		$goodsid = I("post.goodsid");
		$Collection = M("Collection");
		$res=$Collection->where("userid ={$userid} AND goodsid ={$goodsid}")->delete();
		if ($res) {
			$this->response("将商品移除收藏列表",true,1,"成功将商品移除收藏列表");
		}else{
			$this->response("将商品移除收藏列表",false,0,"将商品移除收藏列表失败");
		}
	}
}